<template>
    <div class="order_button" v-if="orderItem">
        <div class="buttonlist" v-if="orderType === 1 ">
            <!--未付款(付款，上传凭证，取消订单)，已发货(申请退货)-->
            <!--发货后7天可以申请退换货-->
            <div class="btn font-24" @click="apply" v-if="orderItem.status === 3 && orderItem.diff > 7 && userLevel === 9">退换货申请</div>
            <div class="btn font-24 yellow" @click="payorder"
                 v-if="orderItem.status === 1 && orderItem.payMethod === 1">确认付款
            </div>
            <div class="btn font-24 yellow" @click="gotoDetail"
                 v-if="orderItem.status === 1 && orderItem.payMethod === 2">去上传凭证
            </div>
            <div class="btn font-24" @click="cancel" v-if="orderItem.status === 1">取消订单</div>
            <div class="btn font-24" @click="makeComment" v-if="orderItem.status === 3">评价</div>
            <div class="btn font-24" @click="cOrder(orderItem.id)" v-if="showCheckButton && orderItem.deliverType === 1 && orderItem.status === 2">审核</div>
        </div>
        <div class="buttonlist" v-if="orderType === 2 ">
            <p class="font-26" v-if="orderItem.status === 1" style="color:#f00">退货正在审核中</p>
            <p class="light-grey font-26" v-else>退货成功</p>
        </div>
    </div>
</template>
<script>
    import {cancelOrder, applyRefundOrder, wechatPay, checkOrder} from '_common/services/order'
    import {getMemberInfo} from '_common/services/user.js'
    import {MessageBox} from 'element-ui'
    import wxLogin from '_common/wxLogin.js'
    export default {
        props: {
            orderType: {
                type: Number,
                default: 1 // 1表示普通订单， 2表示退换货订单， 3表示不显示
            },
            orderItem: {
                type: Object,
                default: null
            },
            showCheckButton : {
                type: Boolean,
                default: false
            }
        },
        data () {
          return {
              userLevel: 1,
          }
        },
        async beforeMount() {
            await wxLogin()
        },
        mounted () {
            this.getMinfo()
        },
        methods: {
            getMinfo () {
                getMemberInfo().then(res => {
                    if (res && res.code === 0) {
                        this.userLevel = res.data.agencyLevel
                        console.log(this.userLevel)
                    }
                    else {
                        this.$alertTip(res.msg || '获取失败请稍后再试')
                    }
                }, reject => {
                    this.$alertTip('获取失败请稍后再试')
                })
            },
            cOrder (orderId) {

                MessageBox.confirm('确认进行订单审核?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    MessageBox.confirm('确认发货给代理商?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '驳回',
                        type: 'warning'
                    }).then(() => {
                        checkOrder(orderId, 1).then(res => {
                            if (res.code === 0) {
                                this.$alertTip('该订单审核已通过！')
                                setTimeout(() => {
                                    window.location.reload()
                                }, 1000)
                            }
                            else {
                                this.$alertTip(res.msg || '出错啦，请稍后再试！')
                            }
                        }, rej => {
                            this.$alertTip(rej.msg || '服务器正在更新，请稍后再试！')
                        }).catch(err => {
                            this.$alertTip('服务器正在更新，请稍后再试！')
                        })
                    }, () => {
                        checkOrder(orderId, 0).then(res => {
                            if (res.code === 0) {
                                this.$alertTip('该订单审核被驳回！')
                                setTimeout(() => {
                                    window.location.reload()
                                }, 1000)
                            }
                            else {
                                this.$alertTip(res.msg || '出错啦，请稍后再试！')
                            }
                        }, rej => {
                            this.$alertTip(rej.msg || '服务器正在更新，请稍后再试！')
                        }).catch(err => {
                            this.$alertTip('服务器正在更新，请稍后再试！')
                        })
                    }).catch(() => {
                    });
                }).catch(() => {
                });



            },
            gotoDetail () {
                this.$openRouter({
                    name: 'myOrderdetail',
                    query: {id: this.orderItem.id}
                })
            },
            makeComment() {
                this.$openRouter({
                    name: "comment",
                    query: {
                        goodsId: this.orderItem.id
                    }
                })
            },
            payorder () {
                wechatPay(this.orderItem.payOrderNo)
            },
            apply () {
                MessageBox.confirm('确定要申请退换货吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    if (this.orderType === 1) {
                        applyRefundOrder(this.orderItem.id).then(res => {
                            if (res && res.code === 0) {
                                this.$openRouter({
                                    name: 'refundOrder'
                                })
                            }
                            else {
                                this.$alertTip(res.msg || '申请失败，请稍后再试')
                            }
                        }, reject => {
                            this.$alertTip('申请失败，请稍后再试')
                        })
                    }
                }).catch(() => {
                });
            },
            cancel () {
                MessageBox.confirm('确定取消该订单?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    cancelOrder(this.orderItem.id).then(res => {
                        if (res && res.code === 0) {
                            this.$alertTip('取消成功')
                            setTimeout(() => {
                                window.location.reload()
                            }, 1500)
                        }
                        else {
                            this.$alertTip(res.msg || '取消失败请稍后再试')
                        }
                    }, reject => {
                        this.$alertTip('取消失败请稍后再试')
                    })
                }).catch(() => {
                })

            }
        }
    }
</script>
<style lang="less" scoped>
    @import "../../css/common";
    @import "../../css/commonlayout";

    .order_button {
        width: 100%;
        margin-right: 32px;
        .buttonlist {
            width: 100%;
            .flex(flex-end);
            .btn {
                height: 50px;
                width: 150px;
                border-radius: 25px;
                margin-left: 16px;
                line-height: 50px;
                text-align: center;
                border: 2px solid @color_gray;
                &.yellow {
                    border-color: @color_orange;
                    color: @color_orange;
                }
            }
        }
    }
</style>